package com.xiaoq.shop.service.impl;

import com.xiaoq.shop.entity.SKUEntity;
import com.xiaoq.shop.repository.ISKURepository;
import com.xiaoq.shop.service.ICatalogService;
import com.xiaoq.shop.service.ISKUService;
import com.xiaoq.vo.PagingVo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by chenjh on 2017/11/3.
 */
@Service
public class SKUServiceImpl implements ISKUService {

    @Autowired
    private ISKURepository skuRepository;

    @Autowired
    private ICatalogService catalogService;

    @Override
    public List<SKUEntity> getTicketSkuList(String shopId, PagingVo pagingVo, Integer saleStatus) {
        if(StringUtils.isNotBlank(shopId)){
            Map<String,String> ticketCode2TypeNameMap = catalogService.getTicketCode2TypeMap();
            if(MapUtils.isNotEmpty(ticketCode2TypeNameMap)){
                List<String> ticketCodeList = new ArrayList<>(ticketCode2TypeNameMap.keySet());
                return skuRepository.getTicketSkuList(shopId, ticketCodeList, pagingVo, saleStatus);
            }
        }
        return new ArrayList<>();
    }

    @Override
    public long getTicketSkuListCount(String shopId, PagingVo pagingVo, Integer saleStatus){
        if(StringUtils.isNotBlank(shopId)){
            Map<String,String> ticketCode2TypeNameMap = catalogService.getTicketCode2TypeMap();
            if(MapUtils.isNotEmpty(ticketCode2TypeNameMap)){
                List<String> ticketCodeList = new ArrayList<>(ticketCode2TypeNameMap.keySet());
                return skuRepository.getTicketSkuListCount(shopId, ticketCodeList, pagingVo, saleStatus);
            }
        }
        return 0L;
    }

    @Override
    public Map<String, List<SKUEntity>> getTicketSkuMap(String shopId, PagingVo pagingVo, Integer saleStatus) {
        final Map<String, List<SKUEntity>> resultMap = new LinkedHashMap<>();
        List<SKUEntity> skuEntityList = getTicketSkuList(shopId, pagingVo, saleStatus);
        if(CollectionUtils.isNotEmpty(skuEntityList)){
            final Map<String, List<SKUEntity>> tmpMap = skuEntityList.stream().collect(Collectors.groupingBy(SKUEntity::getSpecifications));
            if(MapUtils.isNotEmpty(tmpMap)){
                //按 skuEntityList 中specifications是的顺序排序
                skuEntityList.forEach(skuEntity -> {
                    String specifications = skuEntity.getSpecifications();
                    if(tmpMap.containsKey(specifications)){
                        resultMap.put(specifications, tmpMap.get(specifications));
                    }
                });
            }
        }
        return resultMap;
    }

    @Override
    public SKUEntity getTicketSku(String shopId, String spuCode, String skuCode) {
        if(StringUtils.isAnyBlank(shopId, spuCode, skuCode)){
            return null;
        }
        SKUEntity skuEntity = new SKUEntity();
        skuEntity.setShopId(shopId);
        skuEntity.setSpuCode(spuCode);
        skuEntity.setSkuCode(skuCode);
        return skuRepository.getSKUByCommon(skuEntity);
    }

    @Override
    public SKUEntity getTicketSkuById(String skuId) {
        if(StringUtils.isBlank(skuId)){
            return null;
        }
        SKUEntity skuEntity = new SKUEntity();
        skuEntity.setId(skuId);
        return skuRepository.getSKUByCommon(skuEntity);
    }

    @Override
    public SKUEntity getInterpreterSkuById(String shopId, String interpreterId) {
        if(StringUtils.isAnyBlank(shopId, interpreterId)){
            return null;
        }
        SKUEntity skuEntity = new SKUEntity();
        skuEntity.setShopId(shopId);
        skuEntity.setInterpreterId(interpreterId);
        return skuRepository.getSKUByCommon(skuEntity);
    }

    @Override
    public boolean save(SKUEntity skuEntity) {
        return skuRepository.save(skuEntity);
    }

    @Override
    public SKUEntity findTicketSku(String shopId, String spuName, String specifications) {
        if(StringUtils.isAnyBlank(shopId, spuName, specifications)){
            return null;
        }
        SKUEntity skuEntity = new SKUEntity();
        skuEntity.setShopId(shopId);
        skuEntity.setSpuName(spuName);
        skuEntity.setSpecifications(specifications);
        return skuRepository.getSKUByCommon(skuEntity);
    }

    @Override
    public boolean updateSkuById(SKUEntity skuEntity) {
        return skuRepository.update(skuEntity);
    }

    @Override
    public List<SKUEntity> getSkuList(String shopId, String spuCode,Integer saleState) {
        if(StringUtils.isAnyBlank(shopId, spuCode)){
            return new ArrayList<>();
        }

        return skuRepository.getSkuList(shopId, Arrays.asList(spuCode),saleState);
    }

    @Override
    public boolean deleteByInterIdAndShopId(String interId, String shopId) {
        return skuRepository.deleteByInterIdAndShopId(interId, shopId);
    }

    @Override
    public List<SKUEntity> getSkuList(String shopId, List<String> spuCodeList,Integer saleState) {
        return skuRepository.getSkuList(shopId, spuCodeList, saleState);
    }

    @Override
    public boolean deleteSkuByShopIdAndSkuIds(String shopId, List<String> skuIdList) {
        return skuRepository.deleteSkuByShopIdAndSkuIds(shopId, skuIdList);
    }

    @Override
    public boolean updateSkuSaleStateByShopIdAndSkuIds(String shopId, List<String> skuIdList, Integer saleState) {
        return skuRepository.updateSkuSaleStateByShopIdAndSkuIds(shopId, skuIdList, saleState);
    }
}
